﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using ProgNetComponentsDx.Controls.Design.Designers;
using ProgNetComponentsDx.Enums;
using DevExpress.Utils;
using System.Windows.Forms.Design;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Repository;

namespace ProgNetComponentsDx.Controls
{
    [Designer(typeof(MarkControlDesigner)),
    DefaultEvent("MarkUnmark")]
    public partial class MarkControl : AutoSizeGroupControl
    {
        public MarkControl()
        {
            InitializeComponent();
            btnMark.MarkControl = this;
            btnUnmark.MarkControl = this;
            this.ValueMarked = true;
            this.ValueUnMarked = false;
            this.Orientation = DefaultOrientation.Horizontal;
        }
        public event MarkHandler MarkUnmark;

        private Point locationMark = new Point(5, 5);
        private Point locationUnMark = new Point(5,5);
        private DefaultOrientation _Orientation;
        private GridColumn _Column;

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        Category(ProgNetComponents.Constants.Categories.ProgNetSettings)]
        public MarkControlSimpleButton ButtonMark
        {
            get { return btnMark; }
        }
        
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        Category(ProgNetComponents.Constants.Categories.ProgNetSettings)]
        public MarkControlSimpleButton ButtonUnMark
        {
            get { return btnUnmark; }
        }

        [Category(ProgNetComponents.Constants.Categories.ProgNetSettings)]
        public GridView GridView
        {
            get;
            set;
        }

        [Category(ProgNetComponents.Constants.Categories.ProgNetSettings),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
        TypeConverter(typeof(ProgNetComponentsDx.Controls.Design.TypeConverters.GridColumnTypeConverter))]
        public GridColumn Column
        {
            get { return _Column; }
            set
            {
                _Column = value;
                if (value != null && value.ColumnEdit is RepositoryItemCheckEdit)
                {
                    this.ValueMarked = (value.ColumnEdit as RepositoryItemCheckEdit).ValueChecked;
                    this.ValueUnMarked = (value.ColumnEdit as RepositoryItemCheckEdit).ValueUnchecked;
                }
            }
        }

        [Category(ProgNetComponents.Constants.Categories.ProgNetSettings)]
        public DefaultOrientation Orientation
        {
            get { return _Orientation; }
            set
            {
                _Orientation = value;
                SetButtonsLocation();
            }
        }

        [Category(ProgNetComponents.Constants.Categories.ProgNetSettings),
        Editor(typeof(ProgNetComponents.Design.Editors.ObjectEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public object ValueMarked
        {
            get;
            set;
        }

        [Category(ProgNetComponents.Constants.Categories.ProgNetSettings),
        Editor(typeof(ProgNetComponents.Design.Editors.ObjectEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public object ValueUnMarked
        {
            get;
            set;
        }

        public override bool ShowCaption
        {
            get
            {
                return base.ShowCaption;
            }
            set
            {
                base.ShowCaption = value;
                SetButtonsLocation();
            }
        }
        public new Locations CaptionLocation
        {
            get
            {
                return base.CaptionLocation;
            }
            set
            {
                base.CaptionLocation = value;
                SetButtonsLocation();
            }
        }
       
        private void SetButtonsLocation()
        {
            if (Orientation == DefaultOrientation.Horizontal)
            {
                int x = 5;
                int y = 5;
                if (ShowCaption)
                {
                    if (CaptionLocation == DevExpress.Utils.Locations.Top || CaptionLocation == Locations.Default)
                        y += 20;
                    else if (CaptionLocation == DevExpress.Utils.Locations.Left)
                        x += 20;
                }
                locationMark = new Point(x, y);
                locationUnMark = new Point(x, y + 5 + btnMark.Height);
                btnMark.Location = locationMark;
                btnUnmark.Location = locationUnMark;
            }
            else if (Orientation == DefaultOrientation.Vertical)
            {
                int x = 5;
                int y = 5;
                if (ShowCaption)
                {
                    if (CaptionLocation == DevExpress.Utils.Locations.Top || CaptionLocation == Locations.Default)
                        y += 20;
                    else if (CaptionLocation == DevExpress.Utils.Locations.Left)
                        x += 20;
                }
                locationMark = new Point(x, y);
                locationUnMark = new Point(x + 5 + btnMark.Width, y);
                btnMark.Location = locationMark;
                btnUnmark.Location = locationUnMark;
            }
        }

        private void btnMark_Click(object sender, EventArgs e)
        {
            Mark(true);
        }

        private void btnUnmark_Click(object sender, EventArgs e)
        {
            Mark(false);
        }

        private void Mark(bool mark)
        {
            if (GridView != null && Column.IsNotNull())
            {
                GridView.SetAllRowCellValue(Column, mark ? ValueMarked : ValueUnMarked);
                if (MarkUnmark != null)
                    MarkUnmark(this, new MarkEventArgs(mark));
            }
        }
    }

    public delegate void MarkHandler(object sender, MarkEventArgs e);

    public class MarkEventArgs : EventArgs
    {
        public bool Mark
        {
            get;
            private set;
        }
        public MarkEventArgs(bool mark)
        {
            Mark = mark;
        }
    }

    [Designer(typeof(MarkControlButtonDesigner))]
    public class MarkControlSimpleButton : SimpleButton
    {
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        Browsable(false)]
        internal MarkControl MarkControl
        {
            get;
            set;
        }
    }
   
}
